package cn.pso.nspsofs;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


public class SortBest_NSPSOFS {
    public void getNSResult(String dataSetName, List<Particle_NSPSOFS> list, String type) throws IOException {
        String path1 = ResourceUtils.getFile("classpath:testfile\\" + dataSetName).getAbsolutePath();
        File f = new File(path1);
        if (!f.exists()) {
            f.mkdir();
        }
        String path = path1 + "\\" + type + dataSetName + "-NS-result.dat";
        File file = new File(path);
        if (file.isFile()) {
            file.delete();
        }
        file.createNewFile();
        PrintWriter pw = new PrintWriter(new FileOutputStream(path, true));

        for (int i = 0; i < list.size(); i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(i).pbest_fitness[1] == list.get(j).pbest_fitness[1]) {
                    if (list.get(i).pbest_fitness[0] <= list.get(j).pbest_fitness[0]) {
                        list.remove(j);
                    } else {
                        list.set(i, list.get(j));
                        list.remove(j);
                    }
                }
            }
        }

        List<Particle_NSPSOFS> nonDomS = new ArrayList<Particle_NSPSOFS>();
        for (int i = 0; i < list.size(); i++) {
            int index = -1;
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).pbest_fitness[0] <= list.get(i).pbest_fitness[0] && list.get(j).pbest_fitness[1] < list.get(i).pbest_fitness[1]) {
                    index = 1;
                } else if (list.get(j).pbest_fitness[0] < list.get(i).pbest_fitness[0] && list.get(j).pbest_fitness[1] <= list.get(i).pbest_fitness[1]) {
                    index = 1;
                }
            }

            for (int k = 0; k < nonDomS.size(); k++) {
                if (nonDomS.get(k).pbest_fitness[0] == list.get(i).pbest_fitness[0] && nonDomS.get(k).pbest_fitness[1] == list.get(i).pbest_fitness[1]) {
                    index = 1;
                }
            }

            if (index == -1) {
                nonDomS.add(list.get(i));
            }
        }

        for (Particle_NSPSOFS p : nonDomS) {
//            System.out.println(p.pbest_fitness[0] + "------" + p.pbest_fitness[1]);
//			System.out.println(p.pbest_pbest_fitness[0]+"------"+p.pbest_pbest_fitness[1]);

            List<String> choose = new ArrayList<>();
            for (int i = 0; i < p.pbest.length; i++) {
                if (p.pbest[i] > 0.6) {
                    choose.add(String.valueOf(i + 1));
                }
            }

            for (int i = 0; i < choose.size(); i++) {
                if (i == choose.size() - 1) {
                    pw.write(String.valueOf(choose.get(i)));
                    continue;
                }
                pw.write(choose.get(i) + " ");
            }
            pw.write("*" + p.pbest_fitness[0] + " " + p.pbest_fitness[1]);
            pw.write("\n");
            pw.flush();
        }
        pw.close();
        System.out.println(dataSetName + ":NSdone!");

    }
}
